package mylab.io.core.codec;

import java.util.List;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.DatagramPacketDecoder;
import mylab.io.base.model.Msg;

@ChannelHandler.Sharable
public class UdpDecoder<M extends Msg> extends DatagramPacketDecoder {
    public static final String HANDLER_NAME = UdpDecoder.class.getSimpleName();

    public UdpDecoder(SimpleDecoder<M> simpleDecoder) {
        super(simpleDecoder);
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, DatagramPacket datagramPacket, List<Object> out) throws Exception {
        super.decode(ctx, datagramPacket, out);

        //记录每个udp client 的socket端口信息
        for (Object obj : out) {
            if (obj instanceof Msg msg) {
                msg.setTerminal(datagramPacket.sender(), datagramPacket.recipient());
            }
        }

    }
}
